<template>
  <div class="container">
    <div class="hd">
      <div class="hd-left">
        <el-input
          placeholder="请输入套餐名称"
          size="small"
          clearable
          v-model="searchName"
          style="width: 200px; margin-right: 10px"
        ></el-input>
        <TsButton
          perms="combo:page:get"
          label="搜索"
          type="success"
          size="small"
          @click="search"
        ></TsButton>
      </div>
      <div class="hd-right">
        <TsButton
          perms="combo:post"
          label="添加"
          type="primary"
          size="small"
          @click="add"
        ></TsButton>
        <TsButton
          label="删除"
          perms="combo:delete"
          type="danger"
          size="small"
          @click="delMute"
        ></TsButton>
      </div>
    </div>
    <!--    表格区域-->
    <div
      class="table"
      style="height: calc(100% - 160px); margin-top: 20px; overflow-y: auto"
    >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#FEF6F6' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="comboName"
          label="套餐名称"
          width="200"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="comboPrice" label="套餐餐标(元)" width="200">
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="240px">
          <template #default="scope">
            <TsButton
              perms="combo:$id:get"
              style="color: #3cb6e6"
              type="text"
              size="mini"
              label="详情"
              @click="details(scope.row.comboId)"
            ></TsButton>
            <TsButton
              perms="combo:$id:put"
              style="color: #e6a23c"
              type="text"
              size="mini"
              label="修改"
              @click="update(scope.row.comboId)"
            ></TsButton>
            <TsButton
              perms="combo:delete"
              style="color: #f56c6c"
              type="text"
              size="mini"
              label="删除"
              @click="del(scope.row.comboId)"
            ></TsButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
  </div>
</template>

<script setup>
import { mealList, delMeal } from "@/api/canteen/meal";
import commonPageRepository from "@/composables/common-page-repository";
import { onMounted, ref } from "vue";

import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";

const router = useRouter();
//分页查询数据
const pageNum = ref(1);
const pageSize = ref(50);
const totalNum = ref(0);
const searchName = ref("");
//表格数据
const tableData = ref([]);
//获取表格数据
function getMealList() {
  mealList({
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getMealList
);
//搜索方法
function search() {
  mealList({
    comboName: searchName.value,
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}

//添加套餐
function add() {
  router.push({
    name: "addMeal",
  });
}
//多选删除
function delMute() {
  if (muteArr.value.length === 0) {
    ElMessage.warning("请先选择数据");
    return;
  }
  delMeal({
    ids: muteArr.value,
  }).then((res) => {
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getMealList();
    }
  });
}
//多选数据
const muteArr = ref([]);
//多选方法
function handleSelectionChange(val) {
  muteArr.value = val.map((item) => item.comboId).join(",");
}
//删除
function del(id) {
  ElMessageBox.confirm("您确定要删除这条数据么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delMeal({
        ids: id,
      }).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          getMealList();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
//文本信息
//跳转到详情页面
function details(id) {
  router.push({
    name: "mealDetail",
    query: {
      id,
    },
  });
}
function update(id) {
  router.push({
    name: "addMeal",
    query: {
      id,
    },
  });
}
onMounted(() => {
  getMealList();
});
</script>

<style scoped lang="scss">
.container {
  height: calc(100vh - 200px);
  background: #ffffff;
  box-shadow: 0 0 16px rgba(96, 96, 96, 0.16);
  border-radius: 6px;
  padding: 20px;
  overflow-y: auto;
  margin-top: 20px;
  .hd {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
